<script setup lang="ts">
import { computed, reactive ,ref } from 'vue';
import { $t } from '@/locales';
import { loginModuleRecord } from '@/constants/app';
import { useRouterPush } from '@/hooks/common/router';
import { useAntdForm, useFormRules } from '@/hooks/common/form';
import { useAuthStore } from '@/store/modules/auth';
import { fetchGetCaptcha } from '@/service/api';

defineOptions({
  // 账号密码登陆
  name: 'PwdLogin'
});

const authStore = useAuthStore();
const { toggleLoginModule } = useRouterPush();
const { formRef, validate } = useAntdForm();

interface FormModel {
  userName: string;
  password: string;
  captcha: string;
  captchaKey: string;
}

const model: FormModel = reactive({
  userName: 'everwin',
  password: 'ever8807',
  captcha: '',
  captchaKey: ''
});

const captchaUrl = ref('');

async function refreshCaptcha() {
  const { data: captchaInfo, error } = await fetchGetCaptcha();

  if (!error) {
    captchaUrl.value = captchaInfo.captcha;

    model.captchaKey = captchaInfo.captchaKey;
  }
}

// 页面创建时就执行方法
refreshCaptcha();
const visible = ref(false);

const rules = computed<Record<keyof FormModel, App.Global.FormRule[]>>(() => {
  // inside computed to make locale reactive, if not apply i18n, you can define it without computed
  const { formRules } = useFormRules();

  return {
    userName: formRules.userName,
    password: formRules.pwd,
    captcha: formRules.captcha
  };
});

async function handleSubmit() {
  await validate();
  // await authStore.login(model.userName, model.password);
  // 用户名 密码 验证码登陆
  await authStore.loginWithCaptcha(model.userName, model.password, model.captcha, model.captchaKey);
}
</script>

<template>
  <AForm ref="formRef" :model="model" :rules="rules" @keyup.enter="handleSubmit">
    <AFormItem name="userName">
      <AInput v-model:value="model.userName" size="large" :placeholder="$t('page.login.common.userNamePlaceholder')" />
    </AFormItem>

    <AFormItem name="password">
      <AInputPassword
        v-model:value="model.password"
        size="large"
        :placeholder="$t('page.login.common.passwordPlaceholder')"
      />
    </AFormItem>

    <AFormItem name="captcha">
      <ARow>
        <ACol flex="auto">
          <AInput v-model:value="model.captcha" size="large" max-w-80="max-w-80"
                  :placeholder="$t('page.login.common.codePlaceholder')"
                  @keyup.enter="handleSubmit">
          </AInput>
        </ACol>
        <ACol flex="132px">
          <AImage width="150px" height="45px" :preview="visible" :src="captchaUrl" @click="refreshCaptcha" />
        </ACol>
      </ARow>
    </AFormItem>

    <ASpace direction="vertical" size="large" class="w-full">
      <div class="flex-y-center justify-between">
        <ACheckbox>{{ $t('page.login.pwdLogin.rememberMe') }}</ACheckbox>
        <AButton type="text" @click="toggleLoginModule('reset-pwd')">
          {{ $t('page.login.pwdLogin.forgetPassword') }}
        </AButton>
      </div>
      <AButton type="primary" block size="large" shape="round" :loading="authStore.loginLoading" @click="handleSubmit">
        {{ $t('common.confirm') }}
      </AButton>
      <div class="flex-y-center justify-between">
        <AButton class="h-34px flex-1" block @click="toggleLoginModule('code-login')">
          {{ $t(loginModuleRecord['code-login']) }}
        </AButton>
        <div class="w-12px"></div>
        <AButton class="h-34px flex-1" block @click="toggleLoginModule('register')">
          {{ $t(loginModuleRecord.register) }}
        </AButton>
      </div>
    </ASpace>
  </AForm>
</template>

<style scoped></style>
